import * as types from "./mutation-types";
import Api from "api/api";

const state = {
  scoreRecordList: {},
  currentScoreRecord: {},
  sysScore: {},
  searchScoreRecordCondition: {
    pageSize: 10,
    type: "",
    startTime: "",
    endTime: "",
    userName: ""
  }
};

const getters = {
  sysScore: state => state.sysScore,
  scoreRecordList: state => state.scoreRecordList,
  currentScoreRecord: state => state.currentScoreRecord,
  searchScoreRecordCondition: state => state.searchScoreRecordCondition
};

const actions = {
  getScoreRecordPageS({ commit }, param) {
    return Api.getScoreRecordPage(param).then(ret => {
      commit(types.UPDATE_SCORERECORD_LIST, ret.data.data);
      return ret;
    });
  },
  getScoreStatS({ commit }, param) {
    return Api.getScoreStat(param).then(ret => {
      commit(types.UPDATE_SCORER_STAT, ret.data.data);
      return ret;
    });
  },
  getScoreRecordS({ commit }, id) {
    return Api.getScoreRecord({ id: id }).then(ret => {
      commit(types.UPDATE_CURRENT_SCORERECORD, ret.data.data);
      return ret;
    });
  },
  addOrUpdateScoreRecordS({ commit }, payload) {
    return Api.addOrUpdateScoreRecord({ body: payload }).then(ret => {
      return ret;
    });
  },

  deleteScoreRecordS({ commit }, param) {
    return Api.deleteScoreRecord(param).then(ret => {
      return ret;
    });
  }
};

const mutations = {
  [types.UPDATE_SCORERECORD_LIST](state, payload) {
    state.scoreRecordList = payload;
  },
  [types.UPDATE_SCORER_STAT](state, payload) {
    state.sysScore = payload;
  },
  [types.UPDATE_CURRENT_SCORERECORD](state, payload) {
    state.currentScoreRecord = payload;
  }
};

export default {
  state,
  getters,
  actions,
  mutations
};
